{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 31.面向对象编程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31.1 类的定义方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    nationality = 'China' \n",
    "    _deposit=10e10  \n",
    "    __gender=\"M\" \n",
    "    \n",
    "    def __init__(self, name, age):\n",
    "        self.name = name \n",
    "        age = age \n",
    "    \n",
    "    def say_hi(self):\n",
    "        print(self.name)\n",
    "\n",
    "p1 = Person('Tom', 30)\n",
    "p1.say_hi()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 31.2 类中的特殊方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    \"\"\"\n",
    "      此处为类Person的docString\n",
    "      \n",
    "    \"\"\"\n",
    "    nationality = 'China' \n",
    "    _deposit=10e10\n",
    "    __gender=\"M\"\n",
    "    \n",
    "    \n",
    "    def __init__(self, name, age):   \n",
    "        age = age \n",
    "        self.name = name \n",
    "    \n",
    "    def say_hi(self):\n",
    "        print(self.name)\n",
    "\n",
    "    @classmethod\n",
    "    def class_func(cls):\n",
    "        cls.nationality = 'CHINA'\n",
    "        print('I live in {0}'.format(cls.nationality))\n",
    "\n",
    "    @staticmethod\n",
    "    def static_func(x, y):\n",
    "        print(x+y)\n",
    "        \n",
    "p1 = Person('Tom', 20)\n",
    "p1.say_hi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500\n"
     ]
    }
   ],
   "source": [
    "Person.static_func(200,300) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500\n"
     ]
    }
   ],
   "source": [
    "p1.static_func(200,300) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I live in CHINA\n"
     ]
    }
   ],
   "source": [
    "Person.class_func() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I live in CHINA\n"
     ]
    }
   ],
   "source": [
    "p1.class_func()  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 31.3类之间的继承关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Teacher(Person):\n",
    "    pass\n",
    "\n",
    "t1=Teacher(\"zhang\",20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I live in CHINA\n"
     ]
    }
   ],
   "source": [
    "Person.class_func()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I live in CHINA\n"
     ]
    }
   ],
   "source": [
    "t1.class_func()        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n"
     ]
    }
   ],
   "source": [
    "t1.static_func(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "Person.static_func(2,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100000000000.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1._deposit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Teacher' object has no attribute '__gender'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-d2490a499a72>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__gender\u001b[0m \u001b[1;31m#报错信息如下：AttributeError: 'Teacher' object has no attribute '__gender'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m#【提示】子类不能继承父类的private属性__gender\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Teacher' object has no attribute '__gender'"
     ]
    }
   ],
   "source": [
    "t1.__gender "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Person?\n",
    "Teacher?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Person'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Person.__name__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31.4 私有属性及@property 装饰器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Zhang\n"
     ]
    }
   ],
   "source": [
    "class Student:\n",
    "    __name=\"Zhang\"\n",
    "    age=18\n",
    "    @property\n",
    "    def get_name(self):\n",
    "        print(self.__name) \n",
    "      \n",
    "stdnt1=Student()  \n",
    "   \n",
    "stdnt1.get_name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 31.5 self 和 cls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('song', 30, 'zhang', 20)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Student:\n",
    "    age=0\n",
    "    name=\"z\"\n",
    "    def __init__(self):         \n",
    "        self.name=\"zhang\" \n",
    "        age=10 \n",
    "\n",
    "s1=Student()\n",
    "s2=Student()\n",
    "s1.name=\"song\"\n",
    "s1.age=30\n",
    "Student.age=20 \n",
    "Student.name=\"li\" \n",
    "s1.name, s1.age,s2.name, s2.age"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31.6 new与init的区别和联系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new函数被调用\n"
     ]
    }
   ],
   "source": [
    "class Student:\n",
    "    name=\"wang\"\n",
    "    __age=16\n",
    "    \n",
    "    def __new__(cls,name,age): \n",
    "        print('new函数被调用')\n",
    "\n",
    "    def __init__(self,name,age):  \n",
    "        print( 'init函数被调用')\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def sayHi(self):\n",
    "        print(self.name,self.age)\n",
    "        \n",
    "s1= Student(\"zhang\", 18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(s1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'sayHi'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-73bde43db512>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msayHi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m#【提示】报错，AttributeError: 'NoneType' object has no attribute 'sayHi'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'sayHi'"
     ]
    }
   ],
   "source": [
    "s1.sayHi() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new函数被调用\n",
      "init函数被调用\n",
      "zhang 18\n"
     ]
    }
   ],
   "source": [
    "class Student:\n",
    "    name=\"wang\"\n",
    "    __age=16\n",
    "    \n",
    "    def __new__(cls,*args, **kwargs):\n",
    "        print('new函数被调用')\n",
    "        return object.__new__(cls)  \n",
    "\n",
    "    def __init__(self,name,age):\n",
    "        print( 'init函数被调用')\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        \n",
    "    def sayHi(self):\n",
    "        print(self.name,self.age)\n",
    "        \n",
    "s1= Student(\"zhang\", 18)\n",
    "s1.sayHi()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
